﻿

namespace Profit.Controls
{
    using System.Collections.Specialized;
    using System.Windows;
    using System.Windows.Controls;
    using System.Collections.ObjectModel;
    using Diagnostic;
    using System;
    using System.Linq;
    using System.Windows.Media.Animation;
    using System.Diagnostics;
    
    /// <summary>
    /// Interaction logic for EventsViewerControl.xaml
    /// </summary>
    public partial class EventsViewerControl : UserControl
    {
        public static DependencyProperty ShowPopupProperty = DependencyProperty.Register("ShowPopup", typeof(bool), typeof(EventsViewerControl), new PropertyMetadata((bool)false, OnShowPopupChanged));
        public static DependencyProperty EventsSourceProperty = DependencyProperty.Register("EventsSource", typeof(ObservableCollection<TraceEventArgs>), typeof(EventsViewerControl), new PropertyMetadata((ObservableCollection<TraceEventArgs>)null,OnEventsSourceChanged));
        public static DependencyProperty LastEventProperty = DependencyProperty.Register("LastEvent", typeof(TraceEventArgs), typeof(EventsViewerControl), new PropertyMetadata((TraceEventArgs)null));
        public static DependencyProperty ShouldPopupOpenProperty = DependencyProperty.Register("ShouldPopupOpen", typeof(bool), typeof(EventsViewerControl), new PropertyMetadata((bool)false));
        public static DependencyProperty TraceEventTypeFilterProperty = DependencyProperty.Register("TraceEventTypeFilter", typeof(TraceEventType), typeof(EventsViewerControl), new PropertyMetadata(TraceEventType.Information));

        public EventsViewerControl()
        {
            InitializeComponent();
        }

        public TraceEventType TraceEventTypeFilter
        {
            get { return (TraceEventType)GetValue(TraceEventTypeFilterProperty); }
            set { SetValue(TraceEventTypeFilterProperty, value); }
        }

        public bool ShouldPopupOpen
        {
            get { return (bool)GetValue(ShouldPopupOpenProperty); }
            set { SetValue(ShouldPopupOpenProperty,value); }
        }

        public bool ShowPopup
        {
            get { return (bool)GetValue(ShowPopupProperty); }
            set { SetValue(ShowPopupProperty, value); }
        }

        public ObservableCollection<TraceEventArgs> EventsSource
        {
            get { return (ObservableCollection<TraceEventArgs>)GetValue(EventsSourceProperty); }
            set { SetValue(EventsSourceProperty, value); }
        }

        public TraceEventArgs LastEvent
        {
            get { return GetValue(LastEventProperty) as TraceEventArgs; }
            set
            {
                SetValue(LastEventProperty,value);
                var sb = (Storyboard)FindResource("TextFadingStoryBoard");
                sb.Begin(Root);
            }
        }

        static void OnEventsSourceChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null) return;

            var ctrl = o as EventsViewerControl;
            if(ctrl ==null)return;

            var collection = (ObservableCollection<TraceEventArgs>) e.NewValue;
            collection.CollectionChanged += ctrl.OnCollectionChanged;

            if (!collection.Any()) return;

            var evt = collection.Last();
            if (evt.EventType.CompareTo(ctrl.TraceEventTypeFilter) <=0)
                ctrl.LastEvent = evt;
        }

        static void OnShowPopupChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            var ctrl = o as EventsViewerControl;
            if (ctrl == null) return;

            ctrl.ShouldPopupOpen = (bool)e.NewValue && ctrl.EventsSource.Count > 0;
        }

        void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if(e.NewItems == null) return;
            if(e.Action == NotifyCollectionChangedAction.Add)
            {
                if(e.NewItems.Count > 0)
                {
                    var item = e.NewItems[0] as TraceEventArgs;
                    if (item == null) return;
                    
                    if( item.EventType.CompareTo(TraceEventTypeFilter) <= 0 )
                        LastEvent = item;
                }
            }
        }

    }
}
